<?php
class UpdatesController extends AppController {

	var $name = 'Updates';
	var $uses = array('User','Update');

    function beforeFilter () {
        parent::beforeFilter();

		// Aplico el Layout segun la Vista o si es AJAX
		if ($this->RequestHandler->isAjax()) {
			$this->layout = 'ajax';
		}else{
			// Cambio la Vista por la del Rol
			$this->layout = strtolower($this->Session->read('VistaActual'));
		}
    }
    
	function index() {
		$this->Update->recursive = 0;
		$this->set('updates', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid update', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('update', $this->Update->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Update->create();
			if ($this->Update->save($this->data)) {
				$this->Session->setFlash(__('The update has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The update could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid update', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Update->save($this->data)) {
				$this->Session->setFlash(__('The update has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The update could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Update->read(null, $id);
		}
	}

	function actualizarusuario() {

		// Determino el Usuario
		$this->User->id = $this->Session->read('Auth.User.id');

		// Determino el ID de la Ultima Actualizacion
		$mensaje_update = $this->Session->read('UltimoUpdateUsuario_'.$this ->Session->read('Auth.User.id'));

		// Actualizo el Usuario
		$this->User->saveField('update_id', $mensaje_update['Update']['id'] );
		$this->Session->write('Auth.User.update_id', $mensaje_update['Update']['id'] );

		$this->autoRender=false;
	}
}
?>